package com.android;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.AnimationDrawable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class DrawCanvasView extends View implements OnTouchListener{
	
	private Paint paint = new Paint();
	private Paint paintGreen = new Paint();
	private Bitmap bmp;
	private ArrayList<Float> points = new ArrayList<Float>();
	private boolean fromUser = false;
	
	
	private float x = 10;
	private float y = 10;
	
	
	private final static int LINE_WIDTH = 10;
	private final static int CIRCLE_RADIUS_MAX = 30;
	private final static int CIRCLE_RADIUS_MIN = 20;
	private final static int MIN_SIZE = 70;
	private final static int AREA_SIZE = 300;
	
	boolean green = true;
	Animation anim;
	ScaleAnimation scale;
    AnimationSet spriteAnimation;
    float centerX;
    float centerY;
	 ImageView iv;
	public DrawCanvasView(Context context) {
		super(context);
		initData(context);
	}	
	
	public DrawCanvasView(Context context, AttributeSet attrs){
		super(context, attrs);
		initData(context);
	}
	
	public DrawCanvasView(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		initData(context);		
	}
	
	private void initData(Context context){
		setFocusable(true);
		setFocusableInTouchMode(true);			
        this.setOnTouchListener(this);
                
		paint.setStrokeWidth(LINE_WIDTH);
		paint.setAntiAlias(true);
		paint.setColor(Color.RED);		
		
		paintGreen.setAntiAlias(true);
		paintGreen.setColor(Color.GREEN);
		
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green_mark_30);
		 iv = new ImageView(context);
		 iv.setImageBitmap(bmp);
			
	}
	
    @Override    
	protected void onDraw(Canvas canvas){
//		super.onDraw(canvas);				
		
		if(!fromUser){			
			Random r = new Random();
			int bit = r.nextInt(3) + 4;									
			float xStart, yStart, xEnd, yEnd;
			
			xStart = r.nextInt(AREA_SIZE) + 50;
			yStart = r.nextInt(AREA_SIZE) + 50;
						
//			canvas.drawCircle(xStart, yStart, CIRCLE_RADIUS_MAX, paintGreen);			
//			canvas.drawCircle(xStart, yStart, CIRCLE_RADIUS_MIN, paint);
	
			int a = 0;
			int mCase = r.nextInt(2);
			double d = 0;
			for(int i = 0; i < bit; i ++){
				switch(mCase){
				case 0: // change y, const - x
					
					points.add(xStart);					
					points.add(yStart);
					
					xEnd = xStart;
					yEnd = r.nextInt(AREA_SIZE) + 50;
//					float d = Math.abs(yEnd - yStart);
//					double d;
					
					for (int l = 0; l < points.size();) {														
						float sX = points.get(l++);
						float sY = points.get(l++);						
						
						d = Math.sqrt(Math.pow(xEnd - sX, 2) + Math.pow(yEnd - sY, 2));
						int count = 0;
						int tL = l;
						while(d < MIN_SIZE){
							yEnd = r.nextInt(AREA_SIZE) + 50;
							d = Math.sqrt(Math.pow(xEnd - sX, 2) + Math.pow(yEnd - sY, 2));
							l= 0;
							
							count++;
							if(count > 10){
//								d = MIN_SIZE + 1;
								l = tL;
								break;
							}
//							d = Math.abs(yEnd - yStart);
						}
						
			        }
					
//					double d = Math.sqrt(Math.pow(xEnd - xStart, 2) + Math.pow(yEnd - yStart, 2));
					
//					while(d < MIN_SIZE){
//						yEnd = r.nextInt(AREA_SIZE) + 50;
//						d = Math.sqrt(Math.pow(xEnd - xStart, 2) + Math.pow(yEnd - yStart, 2));
////						d = Math.abs(yEnd - yStart);
//					}
	
					canvas.drawLine(xStart, yStart, xEnd, yEnd, paint);
					canvas.drawCircle(xEnd, yEnd, 20, paintGreen);
					canvas.drawCircle(xEnd, yEnd, 10, paint);	
					canvas.drawText(String.valueOf(a), xEnd, yEnd, paint);
										
//					points.add(xStart);					
//					points.add(yStart);
					points.add(xEnd);
					points.add(yEnd);					
					
					xStart = xEnd;
					yStart = yEnd;
					
					a++;
					mCase = 1;
					
					break;				
				case 1: //change x, const - y
					
					points.add(xStart);
					points.add(yStart);
					
					xEnd = r.nextInt(AREA_SIZE) + 50;
					yEnd = yStart;															
					
					for (int l = 0; l < points.size();) {														
						float sX = points.get(l++);
						float sY = points.get(l++);						
						
						d = Math.sqrt(Math.pow(xEnd - sX, 2) + Math.pow(yEnd - sY, 2));
						int count = 0;
						int tL = l;
						while(d < MIN_SIZE){
							xEnd = r.nextInt(AREA_SIZE) + 50;
							d = Math.sqrt(Math.pow(xEnd - sX, 2) + Math.pow(yEnd - sY, 2));
//							d = Math.abs(yEnd - yStart);
							l = 0;
							
							count++;
							if(count > 10){
//								d = MIN_SIZE + 1;
								l = tL;								
								break;
							}
						}						
			        }
					
//					d = Math.abs(xEnd - xStart);
//					while(d < MIN_SIZE){
//						xEnd = r.nextInt(AREA_SIZE) + 50;
//						d = Math.abs(xEnd - xStart);
//					}
												
					canvas.drawLine(xStart, yStart, xEnd, yEnd, paint);					
					canvas.drawCircle(xEnd, yEnd, 20, paintGreen);
					canvas.drawCircle(xEnd, yEnd, 10, paint);			

//					points.add(xStart);
//					points.add(yStart);
					points.add(xEnd);					
					points.add(yEnd);					 
					
					xStart = xEnd;
					yStart = yEnd;
					
					a++;
					mCase = 0;
					
					break;
				default:
					System.out.println("Default");
					break;
				}
				System.out.println(d);	
			}
			canvas.drawCircle(points.get(0), points.get(1), CIRCLE_RADIUS_MAX, paintGreen);			
			canvas.drawCircle(points.get(0), points.get(1), CIRCLE_RADIUS_MIN, paint);
		}
		else{														
			for (int i = 0; i < points.size();) {			
				float xStart = points.get(i++);
				float yStart = points.get(i++);
				float xEnd = points.get(i++);
				float yEnd = points.get(i++);
				canvas.drawLine(xStart, yStart, xEnd, yEnd, paint);
				canvas.drawCircle(xEnd, yEnd, 20, paintGreen);
				canvas.drawCircle(xEnd, yEnd, 10, paint);
	        }
			
			float Cx = points.get(2);
			float Cy = points.get(3);
			
			if((Math.pow(x - Cx, 2) + Math.pow(y - Cy, 2)) <= Math.pow(CIRCLE_RADIUS_MIN, 2)){
				System.out.println("Re-paint");
				canvas.drawCircle(points.get(2), points.get(3), 10, paintGreen);
			}
						
			canvas.drawCircle(x, y, CIRCLE_RADIUS_MAX, paintGreen);			
			canvas.drawCircle(x, y, CIRCLE_RADIUS_MIN, paint);						
			
		}					
	}
        
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() == MotionEvent.ACTION_MOVE){			
//	        Point point = new Point();
//	        point.x = event.getX();
//	        point.y = event.getY();
	        
	        x = event.getX();
	        y = event.getY();	        
//	        System.out.println(x);
//	        System.out.println(y);
	        
//	        points.add(point);
//	        tr.setRotate(point.x, point.y, 0);
//	        if(y <= AREA_SIZE && y >= 0 && x <= 400 && x >= 0){
	        	fromUser = true;	        
	        	invalidate();
//	        }
        }
		if(event.getAction() == MotionEvent.ACTION_DOWN){			
//			invalidate();
		}
//		invalidate();
		return true;
	}

}











